<!--日志详情--->
<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="84px"
        :rules="formRules"
      >
        <el-form-item label="AppName" prop="appname">
          <el-input
            v-model="formData.appname"
            placeholder="AppName"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="名称" prop="title">
          <el-input
            v-model="formData.title"
            placeholder="请输入名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="注册类型" prop="addressType">
          <el-radio-group v-model="formData.addressType">
            <el-radio :label="1">手动</el-radio>
            <el-radio :label="0">自动</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="注册地址" prop="addressList">
          <el-input
            v-model="formData.addressList"
            placeholder="请输入注册地址"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            maxlength="200"
            show-word-limit
          />
          <div class="form-tips">多个用逗号隔开</div>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import { logDetailCat } from "@/api/system/jobapi";
import Popup from "@/components/popup/index.vue";
import feedback from "@/utils/feedback";
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑" : "新增";
});
const formData = reactive({
  id: undefined,
  appname: "",
  title: "",
  addressType: 0,
  addressList: ""
});

const formRules = {
  appname: [
    {
      required: true,
      message: "请输入AppName",
      trigger: ["blur"]
    }
  ],
  title: [
    {
      required: true,
      message: "请输入名称",
      trigger: ["blur"]
    }
  ]
};

const handleSubmit = async () => {
  await formRef.value?.validate();
  console.log(" handleSubmitmode.value", mode.value);
  debugger;
  mode.value == "edit"
    ? await updateJobGroup(formData)
    : await addJobGroup(formData);
  feedback.msgSuccess("操作成功");
  popupRef.value?.close();
  emit("success");
};

const open = (type = "add") => {
  mode.value = type;
  console.log("open---mode", mode.value);

  popupRef.value?.open();
};

const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  console.log("row", row);

  const data = await detailJobGroup(row.id);
  setFormData(data);
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail
});
</script>
